<?php
namespace app\admin\controller;

use \think\Controller;
use \think\auth\Auth;
use \think\catptcha\Catptcha;
use \think\Request as Request;
use \think\admin\model\rule;
use \think\Session;
use \think\Validate as Validate;

class Base extends Controller {

    public $modul = 'admin/';
    public $base = '/public/index.php';
    public $req= '';
    public function _initialize(){
       $this->req = Request::instance();
//        $modul = $re->module();
//        var_dump($modul);
        // $this->check();
    }

    public function err($msg = '系统错误'){
        $this->assign('msg',$msg);
        return $this->fetch('public/error');
    }

    protected function check(){
        $modul   = $this->req->module();
        $control = $this->req->controller();
        $fun     = $this->req->action();
        $auth    = new Auth;
        $rule = $modul.'-'.$control;
        $uid = 1;
        $result = $auth->check($rule,$uid);
        if(!$result){
            $this->redirect($this->base.'/'.$this->modul.'index/index');
        }
    }

    protected function right($data=[]){
        if(!isset($data['state'])){
            $data['state'] = 1;
        }
        return json_encode($data);
    }
    protected function wrong($data=[]){
        if(!isset($data['state'])){
            $data['state'] = 0;
        }
        return json_encode($data);
    }

    protected function send($arg = []){
        $data['state'] = 0;
        $data['msg'] = 'error';
        $data['data'] = ['info'=>'error'];
        if(!isset($arg['state'])){
            $arg['state'] = 0;
        }else{
            $arg['state'] = 1;
        }
        $data = array_merge($data,$arg);
        return json_encode($data);
    }
}
